package com.meter.basic.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.meter.basic.mapper.BasicMeterMapper;
import com.meter.common.core.domain.Ztree;
import com.meter.common.exception.ServiceException;
import com.meter.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meter.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.meter.basic.mapper.BasicHouseMapper;
import com.meter.basic.domain.BasicHouse;
import com.meter.basic.service.IBasicHouseService;
import com.meter.common.core.text.Convert;

/**
 * 住房信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class BasicHouseServiceImpl extends ServiceImpl<BasicHouseMapper, BasicHouse> implements IBasicHouseService
{

    /**
     * 查询住房信息
     * 
     * @param houseId 住房信息主键
     * @return 住房信息
     */
    @Override
    public BasicHouse selectBasicHouseByHouseId(Long houseId)
    {
        return baseMapper.selectBasicHouseByHouseId(houseId);
    }

    /**
     * 查询住房信息列表
     * 
     * @param basicHouse 住房信息
     * @return 住房信息
     */
    @Override
    public List<BasicHouse> selectBasicHouseList(BasicHouse basicHouse)
    {
        return baseMapper.selectBasicHouseList(basicHouse);
    }

    /**
     * 新增住房信息
     * 
     * @param basicHouse 住房信息
     * @return 结果
     */
    @Override
    public int insertBasicHouse(BasicHouse basicHouse)
    {
        if(StringUtils.isNotNull(basicHouse.getParentId())) {
            BasicHouse parent = baseMapper.selectBasicHouseByHouseId(basicHouse.getParentId());
            basicHouse.setFullName(parent.getFullName() + "-" + basicHouse.getHouseName());
            basicHouse.setAncestors(parent.getAncestors() + "," + basicHouse.getParentId());
            basicHouse.setHouseLevel(parent.getHouseLevel() + 1);
        } else {
            basicHouse.setFullName(basicHouse.getHouseName());
            basicHouse.setAncestors("0");
            basicHouse.setParentId(0L);
            basicHouse.setHouseLevel(1);
        }
        basicHouse.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insert(basicHouse);
    }

    /**
     * 修改住房信息
     * 
     * @param basicHouse 住房信息
     * @return 结果
     */
    @Override
    public int updateBasicHouse(BasicHouse basicHouse)
    {
        if(basicHouse.getParentId() != 0L) {
            BasicHouse parent = baseMapper.selectBasicHouseByHouseId(basicHouse.getParentId());
            basicHouse.setFullName(parent.getFullName() + "-" + basicHouse.getHouseName());
            basicHouse.setAncestors(parent.getAncestors() + "," + basicHouse.getParentId());
            basicHouse.setHouseLevel(parent.getHouseLevel() + 1);
        } else {
            basicHouse.setFullName(basicHouse.getHouseName());
            basicHouse.setAncestors("0");
            basicHouse.setParentId(0L);
            basicHouse.setHouseLevel(1);
        }
        basicHouse.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(basicHouse);
    }

    /**
     * 批量删除住房信息
     * 
     * @param houseIds 需要删除的住房信息主键
     * @return 结果
     */
    @Override
    public int deleteBasicHouseByHouseIds(String houseIds)
    {
        return baseMapper.deleteBatchIds(Arrays.asList(Convert.toLongArray(houseIds)));
    }

    /**
     * 删除住房信息信息
     * 
     * @param houseId 住房信息主键
     * @return 结果
     */
    @Override
    public int deleteBasicHouseByHouseId(Long houseId)
    {
        return baseMapper.deleteById(houseId);
    }

    /**
     * 导入住房信息数据
     *
     * @param houseList 住房信息数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importBaseHouse(List<BasicHouse> houseList, boolean updateSupport, String operName)
    {
        if (StringUtils.isNull(houseList) || houseList.size() == 0)
        {
            throw new ServiceException("导入住房信息数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (BasicHouse basicHouse : houseList)
        {
            try
            {
                Long parentId = 0L;
                if(StringUtils.isNotEmpty(basicHouse.getName1())) {
                    BasicHouse houseInfo = baseMapper.selectOne(Wrappers.lambdaQuery(BasicHouse.class)
                            .eq(BasicHouse::getHouseName, basicHouse.getName1())
                            .eq(BasicHouse::getParentId, 0L));
                    if(StringUtils.isNotNull(houseInfo)) {
                        houseInfo.setHouseLevel(1);
                        houseInfo.setUpdateBy(operName);
                        this.updateBasicHouse(houseInfo);
                        parentId = houseInfo.getHouseId();
                    } else {
                        basicHouse.setHouseName(basicHouse.getName1());
                        basicHouse.setHouseLevel(1);
                        basicHouse.setCreateBy(operName);
                        this.insertBasicHouse(basicHouse);
                        parentId = basicHouse.getHouseId();
                    }
                    if(StringUtils.isNotEmpty(basicHouse.getName2())) {
                        houseInfo = baseMapper.selectOne(Wrappers.lambdaQuery(BasicHouse.class)
                                .eq(BasicHouse::getHouseName, basicHouse.getName2())
                                .eq(BasicHouse::getParentId, parentId));
                        if(StringUtils.isNotNull(houseInfo)) {
                            houseInfo.setHouseLevel(2);
                            houseInfo.setUpdateBy(operName);
                            this.updateBasicHouse(houseInfo);
                            parentId = houseInfo.getHouseId();
                        } else {
                            basicHouse.setHouseId(null);
                            basicHouse.setParentId(parentId);
                            basicHouse.setHouseName(basicHouse.getName2());
                            basicHouse.setHouseLevel(2);
                            basicHouse.setCreateBy(operName);
                            this.insertBasicHouse(basicHouse);
                            parentId = basicHouse.getHouseId();
                        }
                    }
                    if(StringUtils.isNotEmpty(basicHouse.getName3())) {
                        houseInfo = baseMapper.selectOne(Wrappers.lambdaQuery(BasicHouse.class)
                                .eq(BasicHouse::getHouseName, basicHouse.getName3())
                                .eq(BasicHouse::getParentId, parentId));
                        if(StringUtils.isNotNull(houseInfo)) {
                            houseInfo.setHouseLevel(3);
                            houseInfo.setUpdateBy(operName);
                            this.updateBasicHouse(houseInfo);
                            parentId = houseInfo.getHouseId();
                        } else {
                            basicHouse.setHouseId(null);
                            basicHouse.setParentId(parentId);
                            basicHouse.setHouseName(basicHouse.getName3());
                            basicHouse.setHouseLevel(3);
                            basicHouse.setCreateBy(operName);
                            this.insertBasicHouse(basicHouse);
                            parentId = basicHouse.getHouseId();
                        }
                    }
                    if(StringUtils.isNotEmpty(basicHouse.getName4())) {
                        houseInfo = baseMapper.selectOne(Wrappers.lambdaQuery(BasicHouse.class)
                                .eq(BasicHouse::getHouseName, basicHouse.getName4())
                                .eq(BasicHouse::getParentId, parentId));
                        if(StringUtils.isNotNull(houseInfo)) {
                            houseInfo.setHouseLevel(4);
                            houseInfo.setUpdateBy(operName);
                            this.updateBasicHouse(houseInfo);
                            parentId = houseInfo.getHouseId();
                        } else {
                            basicHouse.setHouseId(null);
                            basicHouse.setParentId(parentId);
                            basicHouse.setHouseName(basicHouse.getName4());
                            basicHouse.setHouseLevel(4);
                            basicHouse.setCreateBy(operName);
                            this.insertBasicHouse(basicHouse);
                            parentId = basicHouse.getHouseId();
                        }
                    }
                    if(StringUtils.isNotEmpty(basicHouse.getName5())) {
                        houseInfo = baseMapper.selectOne(Wrappers.lambdaQuery(BasicHouse.class)
                                .eq(BasicHouse::getHouseName, basicHouse.getName5())
                                .eq(BasicHouse::getParentId, parentId));
                        if(StringUtils.isNotNull(houseInfo)) {
                            houseInfo.setHouseLevel(5);
                            houseInfo.setUpdateBy(operName);
                            this.updateBasicHouse(houseInfo);
                        } else {
                            basicHouse.setHouseId(null);
                            basicHouse.setParentId(parentId);
                            basicHouse.setHouseName(basicHouse.getName5());
                            basicHouse.setHouseLevel(5);
                            basicHouse.setCreateBy(operName);
                            this.insertBasicHouse(basicHouse);
                        }
                    }
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、住房地址 " + basicHouse.getName1() + " 导入成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、住房地址不能为空");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、住房资料 " + basicHouse.getName1() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }


    /**
     * 查询住房信息树列表
     * 
     * @return 所有住房信息信息
     */
    @Override
    public List<Ztree> selectBasicHouseTree()
    {
        List<BasicHouse> basicHouseList = baseMapper.selectList(Wrappers.lambdaQuery(BasicHouse.class).lt(BasicHouse::getHouseLevel, 5));
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (BasicHouse basicHouse : basicHouseList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(basicHouse.getHouseId());
            ztree.setpId(basicHouse.getParentId());
            ztree.setName(basicHouse.getHouseName());
            ztree.setTitle(basicHouse.getHouseName());
            ztrees.add(ztree);
        }
        return ztrees;
    }

    /**
     * 查询住房信息树列表
     *
     * @return 所有住房信息信息
     */
    @Override
    public List<Ztree> selectBasicHouseAllTree()
    {
        List<BasicHouse> basicHouseList = baseMapper.selectList(Wrappers.lambdaQuery());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (BasicHouse basicHouse : basicHouseList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(basicHouse.getHouseId());
            ztree.setpId(basicHouse.getParentId());
            ztree.setName(basicHouse.getHouseName());
            ztree.setTitle(basicHouse.getHouseName());
            ztrees.add(ztree);
        }
        return ztrees;
    }
}
